/**
 * Account.java  
 * 
 * Linker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.account;

import linker.Settings;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;

/**
 * The Account Class is to manage account This class is to manage accounts and
 * is where the application get all resources, which includes the only globe
 * buddy list, plugins list and information list.
 * 
 * @version 2008-05-19
 * @author Jianfeng tujf.cn@gmail.com
 * @author AwakenRz awakenrz@gmail.com
 * 
 */

public class Account extends Object {

	/**
	 * The Connection of XMPP.
	 */
	private static XMPPConnection connection;

	/**
	 * The name used by the currendt user.
	 */
	private static String userName = null;

	/**
	 * The constructor of Account class.
	 */
	public Account() {
		// Get XMPPconnection
		connection = Connect.getConnecdtion();

		userName = Settings.getSettings().getValue("localname");
	}

	/**
	 * Get username.User's name can get from XMPPtion,if it return null. Get it
	 * from JID,the part before '@'.
	 * 
	 * @return userName
	 */
	public static String getUserName() {
		if (userName == null) {
			userName = connection.getUser();
			userName = userName.substring(0, userName.indexOf("@"));
		}

		return userName;
	}

	/**
	 * Send packet.
	 * 
	 * @param packet
	 *            The packet to be send.
	 */
	public static void sendPacket(final Packet packet) {
		connection.sendPacket(packet);
	}

	/**
	 * Use to change self name when chatting.
	 * 
	 * @param name
	 *            The name to be replace the current used local name.
	 */
	public static void setLocalName(final String name) {
		userName = name;
	}

	/**
	 * 
	 * @return The simple user id.
	 */
	public static String getUser() {
		String user = connection.getUser();

		user = user.substring(0, user.indexOf("/"));

		return user;
	}

	/**
	 * 
	 * @return The full user id(with JID).
	 */
	public static String getUserFullID() {
		return connection.getUser();
	}
}
